package com.dyh.controller;

import com.dyh.po.family;
import com.dyh.po.familyMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.dyh.service.familyService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@Controller
public class familyController {
    @Autowired
    private familyService familyservice;
    @RequestMapping("familyList")
    public String familyList(Model model){
        List<family> families = familyservice.findAllFamily();
        model.addAttribute("familyList",families);
        return "family/familylist";
    }
    @RequestMapping("familyInf")
    public ModelAndView familyInf(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String familyid = request.getParameter("family_id");
        family familyInf = familyservice.findFamilyById(familyid);
        mv.addObject("family",familyInf);
        mv.setViewName("family/familyInf");
        return mv;
    }
    @RequestMapping("familyAdd")
    public String toFamilyAdd(){
        return "family/familyAdd";
    }
    @RequestMapping("familyAddAction")
    public String addFamily(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String fullname = request.getParameter("familyname");
        String idNo = request.getParameter("idNo");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String national = request.getParameter("national");
        String health = request.getParameter("health");
        String culture = request.getParameter("culture");
        String resAttr = request.getParameter("resAttr");
        String agrRes = request.getParameter("agrRes");
        String marry = request.getParameter("marry");
        String workAdd = request.getParameter("workAdd");
        String telephone = request.getParameter("telephone");
        int rows = familyservice.addFamilyMain(fullname,idNo,sex,birthday,national,health,culture,
                resAttr,agrRes,marry,workAdd,telephone);
        if(rows>0){
            String proId = request.getParameter("proid");
            String cityId = request.getParameter("cityid");
            String villId = request.getParameter("villid");
            String groupId = request.getParameter("groupid");
            String familyId = request.getParameter("familyid");
            String familyNum = request.getParameter("familynum");
            String agrNum = request.getParameter("agrnum");
            String address = request.getParameter("address");
            String logTime = request.getParameter("createtime");
            String logName = request.getParameter("logName");
            rows = familyservice.addFamily(proId,cityId,villId,groupId,familyId,fullname,familyNum,agrNum,address,logTime,logName);
        }if(rows>0){
            return "redirect:/familyList";
        }else {
            return "family/familyAdd";
        }
    }
    @RequestMapping("familyEdit")
    public ModelAndView toFamilyEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String familyId = request.getParameter("family_id");
        family family = familyservice.findFamilyById(familyId);
        familyMain familymain = familyservice.findFamilyInfByName(family.getFamilyName());
        ModelAndView mv = new ModelAndView();
        mv.addObject("family",family);
        mv.addObject("familyMain",familymain);
        mv.setViewName("family/familyEdit");
        return mv;
    }
    @RequestMapping("familyEditAction")
    public String editFamily(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String familyId = request.getParameter("familyid");
        String familyNum = request.getParameter("familynum");
        String agrNum = request.getParameter("agrnum");
        String address = request.getParameter("address");
        String createTime = request.getParameter("createtime");
        String logName = request.getParameter("logName");
        Boolean flag = familyservice.editFamily(familyId,familyNum,agrNum,address,createTime,logName);
        if (flag){
            String fullname = request.getParameter("familyname");
            String idNo = request.getParameter("idNo");
            String sex = request.getParameter("sex");
            String birthday = request.getParameter("birthday");
            String national = request.getParameter("national");
            String health = request.getParameter("health");
            String culture = request.getParameter("culture");
            String resAttr = request.getParameter("resAttr");
            String agrRes = request.getParameter("agrRes");
            String marry = request.getParameter("marry");
            String workAdd = request.getParameter("workAdd");
            String telephone = request.getParameter("telephone");
            flag = familyservice.editFamilyMain(fullname,idNo,sex,birthday,national,health,culture,resAttr,
                    agrRes,marry,workAdd,telephone);
        }if(flag){
            return "redirect:/familyList";
        }else {
            return "family/familyEdit";
        }
    }
    @RequestMapping("familyDeleteAction")
    public String deleteFamily(HttpServletRequest request) {
        String familyId = request.getParameter("family_id");
        Boolean flag = familyservice.deleteFamily(familyId);
        return "redirect:/familyList";
    }
}
